const Mock=require('mockjs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
let data=Mock.mock({
    "list|10":[{
        "id":"@id",
        "name":"@cname",
        "title":"@ctitle",
        "img":"@image(100X100,@color)"
    }]
})

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js',
    clean:true,
  },
  plugins: [new HtmlWebpackPlugin({
    template:'./src/index.html',
    filename: 'index.html',
  })],
  module: {
    rules: [
        //解析css
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      //解析scss
      {
        test: /\.s[ac]ss$/i,
        use: [
          // 将 JS 字符串生成为 style 节点
          'style-loader',
          // 将 CSS 转化成 CommonJS 模块
          'css-loader',
          // 将 Sass 编译成 CSS
          'sass-loader',
        ],
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {},
          },
        ],
      },
      //es6转es5
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      }
    ],
  },
  mode: 'production',
  devServer:{
      open:true,
      hot:true,
      port:8848,
      before(app){
          app.get('/list',(req,res)=>{
              res.send(data.list)
          })
      }
  }
};